package com.colin.app.activity.view.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.colin.app.R;
import com.colin.app.activity.view.ViewContract;
import com.colin.app.activity.view.ViewPresenterImpl;
import com.colin.app.app.AppActivity;
import com.colin.app.bean.DemoBean;
import com.colin.app.fragment.test.TestFragment;
import com.colin.library.help.ToastHelp;

public class AddFragmentActivity extends AppActivity implements ViewContract.ViewView ,TestFragment.OnFragmentBackListener{

    private ViewContract.ViewPresenter mViewPresenter = null;
    private TestFragment fragment_find_tag_xml;
    private TestFragment showFragment;
    @Override
    protected void onDestroy() {
        if (null != mViewPresenter) {
            mViewPresenter.destroy();
            mViewPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void initPresenter() {
        DemoBean demoBean = getIntent().getExtras().getParcelable("clazz");
        mViewPresenter = new ViewPresenterImpl(this, this, demoBean);
    }


    @Override
    public int initViewLayoutID() {
        return R.layout.activity_add_fragment;
    }

    @Override
    public void initView() {

    }
    @Override
    public void onBackPressed() {
        if (showFragment != null && showFragment.onBackPressed()) {
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.remove(showFragment);
            transaction.commit();
            showFragment = null;
        }else {
            super.onBackPressed();
        }
    }
    @Override
    public void initData() {
        mViewPresenter.setData();
    }

    public void addFragment0(View view) {
        AddFragment("Fragment-->>0");
    }

    public void addFragment1(View view) {
        AddFragment("Fragment-->>1");
    }

    public void addFragment2(View view) {
        AddFragment("Fragment-->>2");
    }

    public void hideFragment2(View view) {
        hideFragment("Fragment-->>2");
    }

    public void showFragment2(View view) {
        showFragment("Fragment-->>2");
    }

    public void hideFragment1(View view) {
        hideFragment("Fragment-->>1");
    }

    public void showFragment1(View view) {
        showFragment("Fragment-->>1");
    }


    public void detachFragment2(View view) {
        detachFragment("Fragment-->>2");
    }

    public void attachFragment2(View view) {
        attachFragment("Fragment-->>2");
    }

    public void detachFragment1(View view) {
        detachFragment("Fragment-->>1");
    }

    public void attachFragment1(View view) {
        attachFragment("Fragment-->>1");
    }

    public void isAddFragment2(View view) {
        isAddFragment("Fragment-->>2");
    }


    private void AddFragment(String title) {
        //获取Fragment管理
        FragmentManager manager = getSupportFragmentManager();
        //初始化事务
        FragmentTransaction transaction = manager.beginTransaction();
        //初始化Fragment
        Fragment fragment = TestFragment.newInstance(title);
        //将一个fragment实例添加到Activity的最上层
        //add(int containerViewId, Fragment fragment, String tag);
        //将一个fragment实例从Activity的fragment队列中删除
        //remove(Fragment fragment);
        //替换containerViewId中的fragment实例，注意，它首先把containerViewId中所有fragment删除，然后再add进去当前的fragment
        //replace(int containerViewId, Fragment fragment);
        //添加Fragment
        transaction.add(R.id.frame_fragment_show, fragment, title);
        //回退站
        transaction.addToBackStack(title);
        //添加动画效果
        transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
        //事务提交
        transaction.commit();
    }

    private void hideFragment(String title) {
        //获取Fragment管理
        FragmentManager manager = getSupportFragmentManager();
        //得到Fragment
        Fragment fragment = manager.findFragmentByTag(title);
        //初始化事务
        FragmentTransaction transaction = manager.beginTransaction();
        //隐藏Fragment
        transaction.hide(fragment);
        //设置Tag
        transaction.addToBackStack("hide " + title);
        //添加动画效果
        transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
        //事务提交
        transaction.commit();
    }

    private void showFragment(String title) {
        //获取Fragment管理
        FragmentManager manager = getSupportFragmentManager();
        //得到Fragment
        Fragment fragment = manager.findFragmentByTag(title);
        //初始化事务
        FragmentTransaction transaction = manager.beginTransaction();
        //显示Fragment
        transaction.show(fragment);
        //设置Tag
        transaction.addToBackStack("show " + title);
        //添加动画效果
        transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
        //事务提交
        transaction.commit();
    }

    /**
     * 一方面重建fragment的View，注意是重建！
     * 一方面，将fragment实例添加进container的Add队列中
     *
     * @param title
     */
    private void attachFragment(String title) {
        //获取Fragment管理
        FragmentManager manager = getSupportFragmentManager();
        //得到Fragment
        Fragment fragment = manager.findFragmentByTag(title);
        //初始化事务
        FragmentTransaction transaction = manager.beginTransaction();
        //隐藏Fragment
        transaction.attach(fragment);
        //设置Tag
        transaction.addToBackStack("attach " + title);
        //添加动画效果
        transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
        //事务提交
        transaction.commit();
    }

    /**
     * 一方面删除fragment的View视图；
     * 一方面将fragment从Activity的ADD队列中移除！
     *
     * @param title
     */
    private void detachFragment(String title) {
        //获取Fragment管理
        FragmentManager manager = getSupportFragmentManager();
        //得到Fragment
        Fragment fragment = manager.findFragmentByTag(title);
        //初始化事务
        FragmentTransaction transaction = manager.beginTransaction();
        //显示Fragment
        transaction.detach(fragment);
        //设置Tag
        transaction.addToBackStack("detach " + title);
        //添加动画效果
        transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
        //事务提交
        transaction.commit();
    }

    /**
     * detach()：会将view与fragment分离，将此将view从viewtree中删除！而且将fragment从Activity的ADD队列中移除！
     * 所以在使用detach()后，使用fragment::isAdded()返回的值是false；但此fragment实例并不会删除，
     * 此fragment的状态依然保持着使用，所以在fragmentManager中仍然可以找到，即通过FragmentManager::findViewByTag()仍然是会有值的。
     * <p>
     * attach()：显然这个方法与detach()所做的工作相反，它一方面利用fragment的onCreateView()来重建视图，
     * 一方面将此fragment添加到ADD队列中;这里最值得注意的地方在这里：由于是将fragment添加到ADD队列，
     * 所以只能添加到列队头部，所以attach()操作的结果是，最新操作的页面始终显示在最前面！这也就解释了下面的例子中，为了fragment2 detach()后，当再次attach()后，却跑到了fragment3的前面的原因。还有，由于这里会将fragment添加到Activity的ADD队列中，所以在这里调用fragment::isAdded()将返回True;
     *
     * @param from
     * @param to
     */
    private void switchFragment(Fragment from, Fragment to) {
        //获取Fragment管理
        FragmentManager manager = getSupportFragmentManager();
        //初始化事务
        FragmentTransaction transaction = manager.beginTransaction();
        if (!to.isAdded()) {    // 先判断是否被add过
            transaction.hide(from).add(R.id.frame_fragment_show, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
        } else {
            transaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
        }
    }

    private void isAddFragment(String title) {
        //获取Fragment管理
        FragmentManager manager = getSupportFragmentManager();
        //得到Fragment
        Fragment fragment = manager.findFragmentByTag(title);
        if (null != fragment) {
            ToastHelp.showShort(fragment.getTag() + "\tis\t" + String.valueOf(fragment.isAdded()));
        } else {
            ToastHelp.showShort(title + "\tis\t empty!");
        }
    }

    @Override
    public void onFragmentClick(String title, boolean isBack) {
        ToastHelp.showShort(title + "-->>" + String.valueOf(isBack));
    }

    @Override
    public void setSelectedFragment(TestFragment testFragment) {
        this.showFragment = testFragment ;
    }
}
